/**
 * 
 */
package util;

import java.util.ArrayList;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.LocatorImpl;

/**
 * Cette classe est un Parser XML utilisant SAX. Elle permet de récupérer
 * les classes mentionnées dans un fichier XML.
 * 
 * @author Pratik Bhavsar, Fabrice Reynaud.
 *
 */
public class ParserHandler implements ContentHandler {

	@SuppressWarnings("unused")
	private Locator locator;
	
	private ClassPool pool = ClassPool.getDefault();
	private ArrayList<CtClass> ctClasses = new ArrayList<CtClass>();
	protected String path;
		
	public ParserHandler()
	{
		super();
		locator = new LocatorImpl();
	}	
	
	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#endDocument()
	 */
	@Override
	public void endDocument() throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
	 */
	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
	 */
	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
	 */
	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
	 */
	@Override
	public void setDocumentLocator(Locator locator) {
		this.locator = locator;
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
	 */
	@Override
	public void skippedEntity(String name) throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#startDocument()
	 */
	@Override
	public void startDocument() throws SAXException {
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		if(qName.equalsIgnoreCase("CLASS_LIST"))
			this.path = atts.getValue("path");
		else if(qName.equalsIgnoreCase("CLASS"))
		{	
			try {
				this.ctClasses.add(this.pool.get(atts.getValue("name")));
			} catch (NotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
	 */
	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
	}

	public ArrayList<CtClass> getCtClasses() {
		return ctClasses;
	}

	public void setCtClasses(ArrayList<CtClass> ctClasses) {
		this.ctClasses = ctClasses;
	}

	public ClassPool getPool() {
		return pool;
	}

	public void setPool(ClassPool pool) {
		this.pool = pool;
	}
}
